﻿using System;
using ValidationEngine.Commons;
using ValidationEngine.Commons.Enums;
using ValidationEngine.Commons.Interfaces;

namespace ValidationEngine.ValidationRules
{
    public class CustomActionRule : BaseValidationRule
    {
        private readonly Action<IValidationContext, IValidationInfo> _action;

        protected override IValidationInfo ApplyRule(IValidationContext ctx)
        {
            _action(ctx, Result);
            return Result;
        }

        #region Ctor
        public CustomActionRule(
           Action<IValidationContext, IValidationInfo> action,
           string defErrorText = null,
           RuleApplingContext applyCxt = RuleApplingContext.Always,
           ValidationErrorType errorType = ValidationErrorType.Error)
        {
            if (null == action)
            {
                throw new ArgumentNullException("action");
            }
            this._action = action;
            this.ApplingContext = applyCxt;
            this._defErrorType = errorType;
        } 
        #endregion
    }
}
